feat: auto-detect API keys from env vars for zero-config startup#15
feat: auto-detect API keys from env vars for zero-config startup#15Jah-yee wants to merge 1 commit intoopen-gitagent:mainfrom
Conversation
- Add autoDetectModelFromEnv() function that checks common API key env vars - Supports: ANTHROPIC_API_KEY, OPENAI_API_KEY, GOOGLE_API_KEY, OLLAMA_HOST, etc. - Also checks .env file in current directory - Maps API keys to default models (e.g., ANTHROPIC_API_KEY -> claude-sonnet-4-5) - Updates error message to mention auto-detection option
shreyas-lyzr
left a comment
There was a problem hiding this comment.
Good idea — zero-config startup is a nice DX win. A few issues to address before merging:
Bugs
-
Typo:
ANYSACLE_API_KEYshould beANYSCALE_API_KEY— this will silently never match. -
.envreading usescwd(), not the agent directory.readFileSync(".env")reads from the process working directory, which may not be the agent dir. This should use the agent directory path (consistent with how.envis loaded elsewhere in the codebase). However, the agent dir isn't available at this call site — see design concern below. -
Duplicate iteration logic. When a
.envfile exists, theprocess.envkeys are checked inside the try block. If no.envexists, they're checked again after the catch. This means when.envdoes exist but doesn't contain any matching keys,process.envkeys are still checked in the first loop — so the second loop is dead code in that path. Simpler to load.envvars into a merged object once, then iterate once. -
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYmap to the same model separately. Having just an access key without a secret (or vice versa) would falsely trigger auto-detection. These should be checked as a pair (&¬||).
Design concerns
-
Stale model IDs. Hardcoded model IDs like
claude-sonnet-4-5-20250505andgpt-4o-2024-11-20will go stale. Consider using versionless aliases (claude-sonnet-4-5,gpt-4o) or pulling defaults from a config, so this doesn't need code changes every model release. -
.envparsing doesn't handle quotes or comments. Lines likeANTHROPIC_API_KEY="sk-..."will include the quotes in the value. Lines starting with#will be parsed as key-value pairs. The existing.envparser inserver.tshas the same minimal parsing but at least trims — consider reusing that logic or adding quote stripping and comment handling. -
Function placement. This auto-detection runs in
loadAgent()which doesn't receive the agent directory. The.envfile should be read relative to the agent dir, not cwd. You may want to accept the agent dir as a parameter, or move the.envloading to the caller.
Summary
Adds auto-detection of API keys from environment variables (shell env or .env file) to enable zero-configuration startup. When no model is explicitly configured, gitclaw now checks for common API key environment variables and automatically selects an appropriate default model.
Changes
autoDetectModelFromEnv()function that checks for common API key env varsANTHROPIC_API_KEY,OPENAI_API_KEY,GOOGLE_API_KEY,OLLAMA_HOST,GROQ_API_KEY,MISTRAL_API_KEY,DEEPSEEK_API_KEY,XAI_API_KEY, and more.envfile in the current directoryANTHROPIC_API_KEY->claude-sonnet-4-5)Usage
Users can now run gitclaw without any configuration if they have an API key set:
Or via .env file:
# .env ANTHROPIC_API_KEY=sk-...Fixes #14